using System;
using System.Collections.Generic;

namespace MEA.AE_Data
{
	public class FlashContainer
	{
		public struct XMLFlashConditions
		{
			public string conditions_sw_v;

			public int conditions_ds_v;

			public int conditions_hw_v;

			public string conditions_engType;

			public string conditions_wizard_min;

			public string conditions_wizard_max;

			public string conditions_ecu_sw_min;

			public string conditions_ecu_sw_max;

			public int conditions_ecu_ds_min;

			public int conditions_ecu_ds_max;

			public int conditions_ecu_hw_min;

			public int conditions_ecu_hw_max;

			public float conditions_bus_volts_min;

			public bool conditions_ignore;

			public string min_serial_motor_nr;

			public string max_serial_motor_nr;

			public string conditions_country;

			public string conditions_OEM;

			public float file_fct_version;
		}

		private List<FlashBlock> flashBlockAreas;

		private float file_fct_version;

		private string conditions_sw_v;

		private int conditions_ds_v;

		private int conditions_hw_v;

		private string conditions_engType;

		private string conditions_wizard_min;

		private string conditions_wizard_max;

		private string conditions_ecu_sw_min;

		private string conditions_ecu_sw_max;

		private int conditions_ecu_ds_min;

		private int conditions_ecu_ds_max;

		private int conditions_ecu_hw_min;

		private int conditions_ecu_hw_max;

		private float conditions_bus_volts_min;

		private bool conditions_ignore;

		private string conditions_country;

		private string conditions_OEM;

		private string min_serial_motor_nr;

		private string max_serial_motor_nr;

		private string form1_filename;

		private string form1_id;

		public List<FlashBlock.EFD_Types> TypeList;

		public string fdTypeStr;

		public string Conditions_sw_v
		{
			get
			{
				return conditions_sw_v;
			}
		}

		public int Conditions_ds_v
		{
			get
			{
				return conditions_ds_v;
			}
		}

		public int Conditions_hw_v
		{
			get
			{
				return conditions_hw_v;
			}
		}

		public string Conditions_engType
		{
			get
			{
				return conditions_engType;
			}
		}

		public string Conditions_wizard_min
		{
			get
			{
				return conditions_wizard_min;
			}
		}

		public string Conditions_wizard_max
		{
			get
			{
				return conditions_wizard_max;
			}
		}

		public string Conditions_ecu_sw_min
		{
			get
			{
				return conditions_ecu_sw_min;
			}
		}

		public string Conditions_ecu_sw_max
		{
			get
			{
				return conditions_ecu_sw_max;
			}
		}

		public int Conditions_ecu_ds_min
		{
			get
			{
				return conditions_ecu_ds_min;
			}
		}

		public int Conditions_ecu_ds_max
		{
			get
			{
				return conditions_ecu_ds_max;
			}
		}

		public int Conditions_ecu_hw_min
		{
			get
			{
				return conditions_ecu_hw_min;
			}
		}

		public int Conditions_ecu_hw_max
		{
			get
			{
				return conditions_ecu_hw_max;
			}
		}

		public float Conditions_bus_volts_min
		{
			get
			{
				return conditions_bus_volts_min;
			}
		}

		public bool Conditions_ignore
		{
			get
			{
				return conditions_ignore;
			}
		}

		public string Min_serial_motor_nr
		{
			get
			{
				return min_serial_motor_nr;
			}
		}

		public string Conditions_country
		{
			get
			{
				return conditions_country;
			}
		}

		public string Conditions_OEM
		{
			get
			{
				return conditions_OEM;
			}
		}

		public string Max_serial_motor_nr
		{
			get
			{
				return max_serial_motor_nr;
			}
		}

		public float File_fct_version
		{
			get
			{
				return file_fct_version;
			}
		}

		public List<FlashBlock> FlashBlockAreas
		{
			get
			{
				return flashBlockAreas;
			}
		}

		public void addToFlashContainer(ref XMLFlashConditions conditions)
		{
			conditions_sw_v = conditions.conditions_sw_v;
			conditions_ds_v = conditions.conditions_ds_v;
			conditions_hw_v = conditions.conditions_hw_v;
			conditions_engType = conditions.conditions_engType;
			conditions_wizard_min = conditions.conditions_wizard_min;
			conditions_wizard_max = conditions.conditions_wizard_max;
			conditions_ecu_sw_min = conditions.conditions_ecu_sw_min;
			conditions_ecu_sw_max = conditions.conditions_ecu_sw_max;
			conditions_ecu_ds_min = conditions.conditions_ecu_ds_min;
			conditions_ecu_ds_max = conditions.conditions_ecu_ds_max;
			conditions_ecu_hw_min = conditions.conditions_ecu_hw_min;
			conditions_ecu_hw_max = conditions.conditions_ecu_hw_max;
			conditions_bus_volts_min = conditions.conditions_bus_volts_min;
			conditions_ignore = conditions.conditions_ignore;
			min_serial_motor_nr = conditions.min_serial_motor_nr;
			max_serial_motor_nr = conditions.max_serial_motor_nr;
			file_fct_version = conditions.file_fct_version;
			flashBlockAreas = new List<FlashBlock>();
		}

		public void AddFlashblock(FlashBlock fblock)
		{
			flashBlockAreas.Add(fblock);
		}

		public void AddFlashDataBlock(FlashBlockData block)
		{
			if (file_fct_version <= 0.4f)
			{
				foreach (FlashBlock flashBlockArea in flashBlockAreas)
				{
					if (flashBlockArea.Id == block.Id)
					{
						flashBlockArea.Flashdatablocks.Add(block);
						break;
					}
				}
				return;
			}
			if (!(file_fct_version > 0.4f))
			{
				return;
			}
			foreach (FlashBlock flashBlockArea2 in flashBlockAreas)
			{
				if (flashBlockArea2.fdType == block.fdType)
				{
					flashBlockArea2.Flashdatablocks.Add(block);
					break;
				}
			}
		}

		public FlashBlock.EFD_Types TestFCData()
		{
			FlashBlock.EFD_Types result = FlashBlock.EFD_Types.eFD_NoEFD_Type;
			if (file_fct_version <= 0.4f)
			{
				fdTypeStr = conditions_engType;
				for (int i = 0; i < flashBlockAreas.Count; i++)
				{
					FlashBlock flashBlock = flashBlockAreas[i];
					switch (i)
					{
					case 0:
						flashBlock.fdType = FlashBlock.EFD_Types.eFD_PA;
						foreach (FlashBlockData flashdatablock in flashBlock.Flashdatablocks)
						{
							flashdatablock.fdType = FlashBlock.EFD_Types.eFD_PA;
						}
						break;
					case 1:
						flashBlock.fdType = FlashBlock.EFD_Types.eFD_AWS;
						foreach (FlashBlockData flashdatablock2 in flashBlock.Flashdatablocks)
						{
							flashdatablock2.fdType = FlashBlock.EFD_Types.eFD_AWS;
						}
						break;
					case 2:
						flashBlock.fdType = flashBlock.mkStr2ECUHWType(fdTypeStr, false);
						result = flashBlock.fdType;
						foreach (FlashBlockData flashdatablock3 in flashBlock.Flashdatablocks)
						{
							flashdatablock3.fdType = flashBlock.fdType;
						}
						break;
					}
				}
			}
			else
			{
				TypeList = new List<FlashBlock.EFD_Types>();
				for (int j = 0; j < flashBlockAreas.Count; j++)
				{
					FlashBlock flashBlock = flashBlockAreas[j];
					if (flashBlock.cntOfBlocks != flashBlock.Flashdatablocks.Count)
					{
						throw new Exception("Internal: Count of Flash blocks in areas is wrong");
					}
					TypeList.Add(flashBlock.fdType);
				}
			}
			return result;
		}

		public bool DelNeedlesFCTData(FlashBlock.EFD_Types type)
		{
			bool result = false;
			List<FlashBlock> list = new List<FlashBlock>();
			foreach (FlashBlock flashBlockArea in flashBlockAreas)
			{
				if (flashBlockArea.fdType == FlashBlock.EFD_Types.eFD_PA || flashBlockArea.fdType == FlashBlock.EFD_Types.eFD_AWS)
				{
					list.Add(flashBlockArea);
				}
				else if (flashBlockArea.fdType == type)
				{
					fdTypeStr = flashBlockArea.HWTypesStr[(int)type];
					list.Add(flashBlockArea);
					result = true;
				}
			}
			flashBlockAreas = list;
			return result;
		}
	}
}
